/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package agol.rent.system;

import agol.rent.system.admin.FormAdmin;
import agol.rent.system.admin.dialogLogin;
import agol.rent.system.service.LoginService;
import agol.rent.system.service.LoginServiceImpl;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author AgoL - Daffa
 */
public class RunAdmin {
    
    private static FormAdmin formAdmin;
    private static LoginService loginService;
    
    
    public static FormAdmin getFormAdmin (){
       return formAdmin;
    }
    
    public static LoginService getLoginService(){
       return loginService;
    }
    
    public static void initLogin() {
       if(formAdmin == null ) formAdmin = new FormAdmin();
       boolean notLogin = Boolean.TRUE;       
       while (notLogin){
          notLogin = new dialogLogin().showDialog();
         }
          formAdmin = new FormAdmin();
          formAdmin.setVisible(true);
    }
    
    
    private static void initDatabase(){
        try {
            MysqlDataSource dataSource = new MysqlDataSource();
            
            dataSource.setUser("root");
            dataSource.setPassword("daffa");
            dataSource.setDatabaseName("r_system");
            dataSource.setServerName("localhost");
            dataSource.setPort(3306);
            
            loginService = new LoginServiceImpl(dataSource.getConnection());
            System.out.println(" Koneksi Database Berhasil ");
        } catch (SQLException ex) {
            Logger.getLogger(RunAdmin.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println(" Koneksi Database Gagal :" +ex.getMessage());
        }
    }

    /**
     * @param args the command line arguments
     */
     public static void main(String[] usu) {
        // TODO code application logic here
        initDatabase();
        //WidgetUtilities.invokeLater(new Runnable(){
        
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(RunAdmin.class.getName()).log(Level.SEVERE, null, ex);
                } catch (InstantiationException ex) {
                    Logger.getLogger(RunAdmin.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(RunAdmin.class.getName()).log(Level.SEVERE, null, ex);
                } catch (UnsupportedLookAndFeelException ex) {
                    Logger.getLogger(RunAdmin.class.getName()).log(Level.SEVERE, null, ex);
                } 
                        initLogin();
                
             // java.awt.EventQueue.invokeLater(new Runnable() {

              //  @Override
              //  public void run() {
                
             //   FormAdmin formAdmin = new FormAdmin();
             //   formAdmin.setVisible(true);
                        
         //   }
      //  });
     }
}
